package Java并发编程之美第一章;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 石方旭
 * Date: 2022-11-02
 * Time: 10:20
 */
public class TestDemo1 {
    private static int count = 0;
    private static  final  int size = 100000;

    public static void main(String[] args) {
        Thread t1 = new Thread(()-> {
            for (int i = 0; i < size; i++) {
                count++;
            }
        });
        Thread t2 = new Thread(()->{
            for (int i = 0; i < size; i++) {
                count++;
            }
        });
        t1.start();
        t2.start();
        //等线程执行结束终止线程
        try {
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(count);
    }
    public static void main2(String[] args) {
        Thread t1 = new Thread(()-> {
            synchronized (TestDemo1.class) {
                for (int i = 0; i < size; i++) {
                    count++;
                }
            }
        });
        Thread t2 = new Thread(()->{
            synchronized (TestDemo1.class) {
                for (int i = 0; i < size; i++) {
                    count++;
                }
            }
        });
        t1.start();
        t2.start();
        //等线程执行结束终止线程
        try {
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(count);
    }

}
